package com.utonw.carloan.fragment.applyloan;

import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.sensetime.library.finance.ocr.card.BankCardInfo;
import com.sensetime.library.finance.ocr.card.IdCardInfo;
import com.sensetime.sample.common.BankCardActivity;
import com.sensetime.sample.common.ImageHolder;
import com.utonw.carloan.R;
import com.utonw.carloan.fragment.BaseFragment;
import com.utonw.carloan.function.GlobalLoanFunction;
import com.utonw.carloan.utils.ChooseBarUtils;
import com.utonw.carloan.utils.UIUtils;

/**
 * Created by SEELE on 2017/3/22.
 */

/**
 * 申请贷款流程6：银行卡&电商
 */

public class LoanStepSixFragment extends BaseFragment {
    //是否有信用卡的详细界面
    private LinearLayout mHasCreditCard;
    //是否有信用卡的Flag
    private boolean hasCreditCard = false;

    //三方扫描银行卡
    private ImageView mUploadBankCard;
    private EditText mBankCardNum;
    private EditText mBankName;
    //三方扫描信用卡
    private ImageView mUploadCreditCard;
    private EditText mCreditCardNum;
    private EditText mCreditName;
    //全局方法
    private GlobalLoanFunction mGlobalLoanFunction;


    @Override
    public int setLayout() {
        return R.layout.fragment_applyloan_step_six;
    }

    @Override
    public void initView(View view) {
        mHasCreditCard = (LinearLayout) mActivity.findViewById(R.id.ll_has_credit_card);
        mHasCreditCard.setVisibility(View.GONE);

        //绑定扫描点击
        mUploadBankCard = (ImageView) mActivity.findViewById(R.id.iv_btn_upload_bankcard);
        mUploadCreditCard = (ImageView) mActivity.findViewById(R.id.iv_btn_upload_creditcard);

    }

    @Override
    public void initData() {
        //初始化全局方法
        mGlobalLoanFunction = new GlobalLoanFunction();
        //设置ChooseBar标题
        ChooseBarUtils titleUtils = new ChooseBarUtils(getHoldingActivity()) {
            @Override
            protected void setChooseBarClickListener(int view) {
            }

            @Override
            protected void setChooseBarClickListener(int view, TextView toShow) {

            }

            @Override
            protected void getCheckedViewListener(int includeId, int sTitle, View toCheck) {
                setCheckViews(sTitle, toCheck);
            }

            @Override
            public void onToggleButtonListener(int buttonView, boolean isChecked) {

                switch (buttonView) {
                    case R.id.benrenshifouyouxinyongka:
                        if (isChecked) {
                            mHasCreditCard.setVisibility(View.VISIBLE);
                            hasCreditCard = true;
                        } else {
                            mHasCreditCard.setVisibility(View.GONE);
                            hasCreditCard = false;
                        }
                        break;

                }

            }

            @Override
            public void getEditTextWithTipsListener(int includeId, EditText tempEt1) {

            }

            @Override
            public void onRadioButtonListener(RadioButton rMale, RadioButton rFemale) {

            }

            @Override
            public void onImageChooseBarListener(int view, ImageView mIndicator, TextView mInstruction, int sTitle) {

            }

            @Override
            public void getChangeWordsEditTextListener(int includeId, EditText tempEt1, int sTitle) {

            }

            @Override
            public void setVerificationListener() {

            }
        };
        titleUtils.initChooseBar("0", "0", "accountNum", R.id.jiejikakahao, R.string.apply_page_six_card_num, ChooseBarUtils.ResultState.CHANGE_WORDS, "扫描自动识别");
        titleUtils.initChooseBar("0", "0", "bankName", R.id.kaihuhang, R.string.apply_page_six_open_account_bank_name, ChooseBarUtils.ResultState.CHANGE_WORDS, "扫描自动识别");
        titleUtils.initChooseBar("0", "1", "accountName", R.id.kaihuming, R.string.apply_page_six_account_name, ChooseBarUtils.ResultState.CHANGE_WORDS, "扫描自动识别");
        //是否有信用卡字段，此字段不会加入上传的信息中
        titleUtils.initChooseBar("0", "0", "hasCreditCard", 1, R.id.benrenshifouyouxinyongka, R.string.apply_page_six_has_credit_card, ChooseBarUtils.ResultState.TOGGLE_BUTTON, null);
        titleUtils.initChooseBar("0", "0", "creditCard", R.id.xinyongkahao, R.string.apply_page_six_credit_card_num, ChooseBarUtils.ResultState.CHANGE_WORDS, "选填，扫描自动识别");
        titleUtils.initChooseBar("0", "0", "creditCardBankName", R.id.kaihuhang_credit, R.string.apply_page_six_open_account_bank_name_credit_card, ChooseBarUtils.ResultState.CHANGE_WORDS, "扫描自动识别");

        //绑定死值ID
        mBankCardNum = (EditText) mActivity.findViewById(R.id.jiejikakahao).findViewById(R.id.et_item_please_input_none);
        mBankName = (EditText) mActivity.findViewById(R.id.kaihuhang).findViewById(R.id.et_item_please_input_none);
        mCreditCardNum = (EditText) mActivity.findViewById(R.id.xinyongkahao).findViewById(R.id.et_item_please_input_none);
        mCreditName = (EditText) mActivity.findViewById(R.id.kaihuhang_credit).findViewById(R.id.et_item_please_input_none);

        //上传银行卡监听
        mUploadBankCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mGlobalLoanFunction.showUploadMethodDialog(getContext(),getFragment(),BankCardActivity.BANKCARD);
            }
        });

        //上传信用卡监听
        mUploadCreditCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mGlobalLoanFunction.showUploadMethodDialog(getContext(),getFragment(),BankCardActivity.CREDITCARD);
            }
        });
    }

    @Override
    protected void getIdcardFrontInfo(IdCardInfo idCardInfo) {

    }

    @Override
    protected void getIdcardBackInfo(IdCardInfo idCardInfo) {

    }

    @Override
    protected void getBankcardInfo(Intent cardData) {
        //判断是否是借记卡
        if (!cardData.getStringExtra(BankCardActivity.EXTRA_CARD_TYPE).equals("借记卡")) {
            UIUtils.showShortToast(getString(R.string.please_upload_bankcard));
        } else {
            //向借记卡信息赋值
            int orientation = cardData.getIntExtra(BankCardActivity.EXTRA_CARD_ORIENTATION, BankCardActivity.CARD_ORIENTATION_HORIZONTAL);
            int[] colors = ImageHolder.sImageData;
            if (colors != null && colors.length > 0) {
                Bitmap cardImage = Bitmap.createBitmap(colors,
                        orientation == BankCardActivity.CARD_ORIENTATION_HORIZONTAL ?
                                BankCardInfo.IMAGE_LONG_SIDE :
                                BankCardInfo.IMAGE_SHORT_SIDE,
                        orientation == BankCardActivity.CARD_ORIENTATION_HORIZONTAL ?
                                BankCardInfo.IMAGE_SHORT_SIDE :
                                BankCardInfo.IMAGE_LONG_SIDE,
                        Bitmap.Config.ARGB_8888);
                mUploadBankCard.setImageBitmap(cardImage);
            }
            mBankCardNum.setText(cardData.getStringExtra(BankCardActivity.EXTRA_CARD_NUMBER));
            mBankName.setText(cardData.getStringExtra(BankCardActivity.EXTRA_BANK_NAME));
        }
    }

    @Override
    protected void getCreditcardInfo(Intent cardData) {
        //判断是否是信用卡
        if (cardData.getStringExtra(BankCardActivity.EXTRA_CARD_TYPE).equals("借记卡")) {
            UIUtils.showShortToast(getString(R.string.please_upload_creditcard));
        } else {
            //向信用卡信息赋值
            int orientation = cardData.getIntExtra(BankCardActivity.EXTRA_CARD_ORIENTATION, BankCardActivity.CARD_ORIENTATION_HORIZONTAL);
            int[] colors = ImageHolder.sImageData;
            if (colors != null && colors.length > 0) {
                Bitmap cardImage = Bitmap.createBitmap(colors,
                        orientation == BankCardActivity.CARD_ORIENTATION_HORIZONTAL ?
                                BankCardInfo.IMAGE_LONG_SIDE :
                                BankCardInfo.IMAGE_SHORT_SIDE,
                        orientation == BankCardActivity.CARD_ORIENTATION_HORIZONTAL ?
                                BankCardInfo.IMAGE_SHORT_SIDE :
                                BankCardInfo.IMAGE_LONG_SIDE,
                        Bitmap.Config.ARGB_8888);
                mUploadCreditCard.setImageBitmap(cardImage);
            }
            mCreditCardNum.setText(cardData.getStringExtra(BankCardActivity.EXTRA_CARD_NUMBER));
            mCreditName.setText(cardData.getStringExtra(BankCardActivity.EXTRA_BANK_NAME));
        }

    }


    public boolean hasCreditCard() {
        return hasCreditCard;
    }


}
